package practice5.dao;
import practice5.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
public class StudentDaoImpl {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert() {
        String sql="insert into student(name,gender,clbum,grade) values(?,?,?,?)";
        int result=jdbcTemplate.update(sql,"张三","男","计算机科学与技术","2018");
        return result;
    }
    @Override
    public List<Student> selectAll() {
        String sql="select * from student";
        List<Student> students=jdbcTemplate.query(sql,new BeanPropertyRowMapper<Student>(Student.class));
        return students;
    }
    @Override
    public Student getStudents(String number) {
        String sql = "select * from students where number=?";
        Student students = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), number);
        if(students!=null){
            return students;
        }

        return students;
    }

    @Override
    public int update(Student students) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, students.getNumber(), students.getGender(), students.getClbum(), students.getGrade() , students.getName());
        return i;
    }
}
